#include <Arduino.h>
#include "../include/Traffic_595.h"

int lightstate[4] = {0b1, 0b1, 0b1, 0b1};
String info;

void Update()
{
  int temp = 0;
  for(int i=0;i<4;i++)
  {
    temp <<= 4;
    temp |= lightstate[i];
  }
  Write_595(16, temp);
  Sync_595();
}

void setup()
{
  Init_595();
  Enable_595();
  Serial.begin(115200);
  Update();
}

void loop()
{
  info.clear();
  info = Serial.readString();
  if (info == "East-Red\n")
  {
    lightstate[3] = 0b1;
    Serial.write(0xAA);
  }
  if (info == "East-Yellow\n")
  {
    lightstate[3] = 0b110;
    Serial.write(0xAA);
  }
  if (info == "East-Green\n")
  {
    lightstate[3] = 0b1000;
    Serial.write(0xAA);
  }
  if (info == "South-Red\n")
  {
    lightstate[2] = 0b1;
    Serial.write(0xAA);
  }
  if (info == "South-Yellow\n")
  {
    lightstate[2] = 0b110;
    Serial.write(0xAA);
  }
  if (info == "South-Green\n")
  {
    lightstate[2] = 0b1000;
    Serial.write(0xAA);
  }
  if (info == "West-Red\n")
  {
    lightstate[1] = 0b1;
    Serial.write(0xAA);
  }
  if (info == "West-Yellow\n")
  {
    lightstate[1] = 0b110;
    Serial.write(0xAA);
  }
  if (info == "West-Green\n")
  {
    lightstate[1] = 0b1000;
    Serial.write(0xAA);
  }
  if (info == "North-Red\n")
  {
    lightstate[0] = 0b1;
    Serial.write(0xAA);
  }
  if (info == "North-Yellow\n")
  {
    lightstate[0] = 0b110;
    Serial.write(0xAA);
  }
  if (info == "North-Green\n")
  {
    lightstate[0] = 0b1000;
    Serial.write(0xAA);
  }
  if (info == "Update\n")
  {
    Update();
    Serial.write(0xAA);
  }
}